<?php
class cardModel extends appModel
{
    //获取卡券信息
    public function get_index($description,$id,$page, $limit = 10)
    {
        $listPage=array('data'=>'','pageLink'=>'');
        $where = ' WHERE 1=1 ';
        if(isset($description) && $description!='')
        {
            $where.=' and description like "%'.$description.'%"';
        }
        if(isset($id) && $id!='')
        {
            $where.=' and id = "'.$id.'"';
        }
        $sql = 'SELECT * FROM '.$this->table('card').$where;
        $result=$this->show_page($sql, $page, $limit);
        $listPage['pagelink']=$result['page'];
        $listPage['data']=$result['result'];
        return $listPage;
    }
               
    //保存卡券信息
     public function set_index($card_id,$hour_max,$min_max,$id,$name,$img,$status,$description,$change_integral,$dotime2,$is_chat,$day_max)
    {
        if($is_chat == 1)
        {
            $sql = "UPDATE ".$this->table('card')." SET is_chat='0'";
            $res = $this->query($sql);
        }
        
        
        if(!empty($id)){
            $sql = "UPDATE ".$this->table('card')." SET card_id='$card_id',is_chat='$is_chat',hour_max='$hour_max',min_max='$min_max',name='$name', img='$img',status='$status',description='$description',change_integral='$change_integral',dotime2='$dotime2',day_max='$day_max' WHERE id='".$id."'";
            $res = $this->query($sql);
        }else{
            $sql = "INSERT INTO ".$this->table('card')." (card_id,hour_max,min_max,name,description,img,status,change_integral,dotime2,is_chat,day_max) values ('".$card_id."','".$hour_max."','".$min_max."','".$name."','".$description."','".$img."','".$status."','".$change_integral."','".$dotime2."','".$is_chat."','".$day_max."')";
            $res = $this->query($sql);
        }
        
        
        
        return $res;   
    } 

    public function get_index_one($id)
    {
        $sql = 'SELECT * FROM '.$this->table('card').' WHERE id="'.$id.'"';
        $res = $this->getRow($sql);
        
        $res['dotime2'] = date('Y-m-d ',$res['dotime2']);
        return $res;
    }
    
    //删除指定记录
    public function del_index($id)
    {
        $result=array('error'=>0,'content'=>'删除成功');
        if($id)
        {
            $sql = "DELETE FROM ".$this->table('card')." WHERE id='".$id."'";
            $res=$this->query($sql);
            if(!$res)
            {
                $result['error']=1;
                $result['content']='删除失败';
            }
        }
        return $result;
    }
    
    //获取卡券统计信息
    public function tongji($description,$page, $limit = 10)
    {
        $listPage=array('data'=>'','pageLink'=>'');    
        $where = ' WHERE 1=1 AND a.is_get = 1  ';    
        if(isset($description) && $description!='')
        {
            $where.=' and a.cid = "'.$description.'" group by a.date,a.cid ';
        }
        else{
            $where.= ' group by a.date,a.cid ';
        }
//         $sql = 'SELECT a.cid,a.date,count(a.nums) as nums,b.description FROM '.$this->table('card_get_record').' as a LEFT JOIN '.$this->table('card').' as b ON a.cid=b.id '.$where;
        $sql = 'SELECT a.date,count(*) as nums,b.description FROM '.$this->table('card_get_record').' as a LEFT JOIN '.$this->table('card').' as b ON a.cid=b.id '.$where;
        $result=$this->show_page($sql, $page, $limit,array(),"",1);
        //$result=$this->show_page($sql, $page, $limit);
        $listPage['pagelink']=$result['page'];
        $listPage['data']=$result['result'];
        return $listPage;
    }
    
    //获取所有卡券名称
    public function get_description()
    {
        $sql = 'SELECT description,id FROM '.$this->table('card');
        return $this->getAll($sql);
    }
    
    public function listAll($description)
    {
         $where = ' WHERE 1=1 AND a.is_get = 1  '; 
        if(isset($description) && $description!='')
        {
            $where.=' and b.description like "%'.$description.'%" group by a.date,a.cid ';
        }
        else{
            $where.= ' group by a.date,a.cid ';
        }

        $sql = 'SELECT b.description,count(*) as nums,a.date FROM '.$this->table('card_get_record').' as a LEFT JOIN '.$this->table('card').' as b ON a.cid=b.id '.$where;
//         $result=$this->show_page($sql, $page, $limit,array(),"",1);
//         $listPage['pagelink']=$result['page'];
//         $listPage['data']=$result['result'];

        $res = $this->getAll($sql);
        return $res;
    }
    public function get_user_list($cid,$page=1,$limit = 30)
    {
        $where = " WHERE 1=1 AND  a.cid = $cid ORDER BY a.addtime DESC ";
        $sql = 'SELECT a.num,a.is_get,a.addtime,b.name,b.phone FROM '.$this->table('card_get_record').' a LEFT JOIN '.$this->table('card_user').' as b ON a.wxid=b.wxid '.$where;
        $result=$this->show_page($sql, $page, $limit,array(),"",1);
        $listPage['pagelink']=$result['page'];
        $listPage['data']=$result['result'];
        return $listPage;
    }
     public function getUserAll($cid)
   {
       $where = " WHERE 1=1 AND  a.cid = $cid ORDER BY a.addtime DESC ";
       $sql = 'SELECT b.name,b.phone,a.num,a.is_get,a.addtime FROM '.$this->table('card_get_record').' a LEFT JOIN '.$this->table('card_user').' as b ON a.wxid=b.wxid '.$where;
       $result = $this->getAll($sql);
       foreach ($result as $k=>$v)
       {
           $result[$k]['addtime'] = date("Y-m-d H:i:s",$v['addtime']);
       }
       return $result;
   }
}?>